package org.jeecg.util;

import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.transcoder.Transcoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Document;

import java.io.*;

public class SVGUtils {

    /**
     * SVG转PNG
     *
     * @param svgCode SVG代码
     * @param out     输出流
     * @throws TranscoderException
     * @throws IOException
     */
    public static void svg2PNG(String svgCode, OutputStream out) throws TranscoderException, IOException {
        Transcoder transcoder = new PNGTranscoder();
        svgConverte(svgCode, out, transcoder);
    }

    /**
     * SVG转PNG
     *
     * @param svgFile SVG文件
     * @param outFile 输出文件
     * @throws TranscoderException
     * @throws IOException
     */
    public static void svg2PNG(File svgFile, File outFile) throws TranscoderException, IOException {
        Transcoder transcoder = new PNGTranscoder();
        svgConverte(svgFile, outFile, transcoder);
    }

    private static void svgConverte(String svgCode, String outpath, Transcoder transcoder) throws IOException, TranscoderException {
        svgConverte(svgCode, getOutputStream(outpath), transcoder);
    }

    private static void svgConverte(File svg, File outFile, Transcoder transcoder) throws IOException, TranscoderException {
        svgConverte(svg2String(getInputStream(svg)), getOutputStream(outFile), transcoder);
    }

    private static void svgConverte(String svgCode, OutputStream out, Transcoder transcoder) throws IOException, TranscoderException {
        svgCode = svgCode.replaceAll(":rect", "rect");
        TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(svgCode.getBytes()));
        TranscoderOutput output = new TranscoderOutput(out);
        svgConverte(input, output, transcoder);
    }

    private static void svgConverte(TranscoderInput input, TranscoderOutput output, Transcoder transcoder) throws IOException, TranscoderException {
        transcoder.transcode(input, output);
    }


    public static InputStream getInputStream(File file) throws IOException {
        return new FileInputStream(file);
    }

    public static InputStream getInputStream(String filepath) throws IOException {
        File file = new File(filepath);
        if (file.exists())
            return getInputStream(file);
        else
            return null;
    }

    public static OutputStream getOutputStream(File outFile) throws IOException {
        return new FileOutputStream(outFile);
    }

    public static OutputStream getOutputStream(String outpath) throws IOException {
        File file = new File(outpath);
        if (!file.exists())
            file.createNewFile();
        return getOutputStream(file);
    }

    /**
     * 默认使用编码UTF-8 SVG文件输入流转String
     *
     * @param svgFile
     * @return SVG代码
     * @throws IOException
     */
    public static String svg2String(File svgFile) throws IOException {
        InputStream in = getInputStream(svgFile);
        return svg2String(in, "UTF-8");
    }

    /**
     * 默认使用编码UTF-8 SVG文件输入流转String
     *
     * @param in
     * @return SVG代码
     * @throws IOException
     */
    public static String svg2String(InputStream in) throws IOException {
        return svg2String(in, "UTF-8");
    }

    /**
     * 指定字符集SVG输入流转String
     *
     * @param in      输入流
     * @param charset 字符编码
     * @return SVG代码
     */
    public static String svg2String(InputStream in, String charset) {
        StringBuffer svgBuffer = new StringBuffer();
        BufferedReader bfr = null;
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(in, charset);
            bfr = new BufferedReader(inputStreamReader);
            String line = "";
            while ((line = bfr.readLine()) != null) {
                svgBuffer.append(line);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bfr != null)
                    bfr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return svgBuffer.toString();
    }
}
